<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app"
import { useUserStore } from "@/store/user"
// #ifdef H5
import { reportPageLoadPerformance } from "@/utils/performance"
// #endif

onLaunch(() => {
  console.log("App Launch")
  uni.loadFontFace({
    family: "uicon-iconfont",
    source: 'url("https://at.alicdn.com/t/font_2225171_8kdcwk4po24.ttf")',
    global: true,
    success: (success) => {
      console.log("Font loaded successfully", success);
    },
    fail: (fail) => {
      console.log("Font loading failed", fail);
    },
  });

  // #ifdef H5
  setTimeout(() => {
    reportPageLoadPerformance()
  }, 2000)
  // #endif
})

onShow(() => {
  // 应用从后台回到前台时，可以选择重新检查登录状态
  console.log("App Show")
})

onHide(() => {
})

// 检查token并进行路由跳转
const checkTokenAndRedirect = async () => {
  try {
    const userStore = useUserStore()

    // 检查是否有token
    if (userStore.hasToken) {
      console.log('✅ 发现token，尝试验证有效性')

      try {
        // 尝试获取用户信息验证token有效性
        await userStore.fetchUserInfo()
      
        uni.reLaunch({
          url: '/pages/home/index'
        })
      } catch (error) {
     
        // token无效，清除用户信息并跳转到登录页
        userStore.clearUserInfo()
        uni.reLaunch({
          url: '/pages/login/index'
        })
      }
    } else {
     
    
      uni.reLaunch({
        url: '/pages/login/index'
      })
    }
  } catch (error) {
  
    // 出错时默认跳转到登录页
    console.log('🔑 跳转到登录页（出错）')
    uni.reLaunch({
      url: '/pages/login/index'
    })
  }
}
</script>


<style lang="scss">
@import "uview-plus/index.scss"; 
/* stylelint-disable selector-type-no-unknown */
.zp-l-text-rpx{
font-size: 14px !important;
}
::v-deep{
  .u-form-item__body{
    padding: 24rpx 0 !important;
  }

}
button::after {
  border: none;
}

swiper,
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}

image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

// 单行省略，优先使用 unocss: text-ellipsis
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// 两行省略
.ellipsis-2 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

// 三行省略
.ellipsis-3 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}


</style>
